package com.th.supcom.pds.drugsupply.client.widget;

import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.drugsupply.client.model.DrugInfoModel;

/**
 * 药品输入框控件(用于药品属性维护).
 * 
 * @param <D>
 */
public class DrugComboBoxForM extends ComboBox <DrugInfoModel>
{
    private final IExcuteQuery m_iExcuteQuery;
    private final String m_deptCode;
    private final RpcProxy <PagingLoadResult <DrugInfoModel>> m_proxy = new RpcProxy <PagingLoadResult <DrugInfoModel>> ()
    {
        @Override
        public void load (final Object loadConfig, final AsyncCallback <PagingLoadResult <DrugInfoModel>> callback)
        {
            final PagingLoadConfig config = (PagingLoadConfig) loadConfig;
            final String query = config.get ("query");
            m_iExcuteQuery.query (config, query, m_deptCode, callback);
        }
    };

    private DrugInfoModel m_value;

    public DrugComboBoxForM (final IExcuteQuery query, String deptCode)
    {

        super ();
        m_iExcuteQuery = query;
        m_deptCode = deptCode;
        this.init ();
    }

    /**
     * 获取模版格式 . 设置模版显示的格式为html表格形式，可以自定义为其它格式比如div等 class属性为FcWeb工程中
     * index.css定义的css,如有必要可以进行修改 <tpl for>标签为循环语句 <tpl if>为条件判断语句
     * 
     * @return String
     */
    private native String getCustomTemplate ()
    /*-{
    return [
    ($wnd.pdsSelectTemplate)].join("");
    }-*/;

    @Override
    public DrugInfoModel getValue ()
    {
        return this.m_value;
    }

    private void init ()
    {
        ListStore <DrugInfoModel> listStore;
        // loader加载器
        PagingLoader <PagingLoadResult <ModelData>> loader = new BasePagingLoader <PagingLoadResult <ModelData>> (m_proxy);
        listStore = new ListStore <DrugInfoModel> (loader);
        this.setEmptyText ("请输入药品拼音码...");
        this.setDisplayField ("phamName");
        this.setStore (listStore);
        this.setMinChars (0);
        this.setUseQueryCache (false);
        this.setQueryDelay (500);
        this.setItemSelector ("tr.search-item");
        this.setTemplate (getCustomTemplate ());
        this.setPageSize (10);
        this.setMinListWidth (600);
        this.setTriggerAction (TriggerAction.ALL);

    }

    @Override
    protected void onKeyUp (final FieldEvent fe)
    {
        if (null == this.getInputEl ().getValue () || this.getInputEl ().getValue ().isEmpty ())
        {
            this.setValue (new DrugInfoModel ());
            return;
        }
        super.onKeyUp (fe);
    }

    @Override
    public void setValue (final DrugInfoModel value)
    {
        m_value = value;
        super.setValue (value);
    }

}
